package com.google.fbyoutube.client.widget;

import java.util.List;

import org.adamtacy.client.ui.effects.impl.NShow;

import com.google.fbyoutube.client.page.Home;
import com.google.fbyoutube.shared.ytVideoEntry;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.layout.client.Layout;
import com.google.gwt.layout.client.Layout.Layer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class WidUserOwnVideo extends Composite {

  private HorizontalPanel pagerPanel;
  
  private LayoutPanel videoContainer;

  private Button previousVideos, nextVideos;

  private int MAX_VIDEO_COLUMN = 5;
  
  private int currentPage = 0;
  
  private List<ytVideoEntry> userOwnedVods;
  
  private ToggleButton[] navButtons;
  
  private int THUMB_WIDTH = 130;
  
  DisclosurePanel disclosurePanel;

  public WidUserOwnVideo(ytVideoEntry video) {

    disclosurePanel = new DisclosurePanel("Videos uploaded by " + video.getAuthor(), true);
    disclosurePanel.setAnimationEnabled(true);
    initWidget(disclosurePanel);
    disclosurePanel.setSize("740px", "100%");

    VerticalPanel verticalPanel = new VerticalPanel();
    verticalPanel.setStyleName("panel-round");
    verticalPanel.setBorderWidth(0);
    disclosurePanel.setContent(verticalPanel);
    verticalPanel.setSize("95%", "100%");

    pagerPanel = new HorizontalPanel();
    pagerPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    pagerPanel.setSpacing(1);
    verticalPanel.add(pagerPanel);
    pagerPanel.setWidth("");

    ToggleButton button_3 = new ToggleButton("Loading...");
    pagerPanel.add(button_3);

    HorizontalPanel horizontalPanel = new HorizontalPanel();
    horizontalPanel.setStyleName("nocellspace");
    horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    horizontalPanel.setSize("100%", "4cm");
    verticalPanel.add(horizontalPanel);

    previousVideos = new Button("<");
    previousVideos.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        if (userOwnedVods != null) {
        	showVideoPage (currentPage - 1);
        }
      }
    });
    previousVideos.setStyleName("userOwn-button");
    horizontalPanel.add(previousVideos);
    previousVideos.setSize("20px", "120px");
    previousVideos.setEnabled(false);

    videoContainer = new LayoutPanel();
    horizontalPanel.add(videoContainer);
    videoContainer.setStylePrimaryName("user_video");
    videoContainer.setWidth("650px");
    videoContainer.setHeight("140px");

    nextVideos = new Button(">");
    nextVideos.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        if (userOwnedVods != null) {
        	showVideoPage (currentPage + 1);
        }
      }
    });
    nextVideos.setStyleName("userOwn-button");
    nextVideos.setEnabled(false);
    horizontalPanel.add(nextVideos);
    horizontalPanel.setCellHorizontalAlignment(nextVideos, HasHorizontalAlignment.ALIGN_RIGHT);
    nextVideos.setSize("20px", "120px");

    loadUserVideos(video.getAuthor());
  }

  private void loadUserVideos(String username) {
    Home.videoService.getYTUploadVideos(username,
        new AsyncCallback<List<ytVideoEntry>>() {

          @Override
          public void onSuccess(final List<ytVideoEntry> result) {
            if (result != null && result.size() > 0) {
              userOwnedVods = result;
              int pagerNum = (result.size() - 1) / MAX_VIDEO_COLUMN + 1;

              pagerPanel.clear();
              navButtons = new ToggleButton[pagerNum];
              for (int i = 0; i < pagerNum; ++i) {
                navButtons[i] = new ToggleButton(String.valueOf(i + 1));
                navButtons[i].setDown(false);
                pagerPanel.add(navButtons[i]);
                
                navButtons[i].addClickHandler(new ClickHandler() {

                  @Override
                  public void onClick(ClickEvent event) {
                	  ToggleButton thisButton = (ToggleButton) event.getSource();
                	  showVideoPage(Integer.valueOf(thisButton.getText()) - 1);
                  }
                });
              }
              
              for (int i = 0; i < result.size(); ++i) {
        	      WidThumb widThumb;
        	      widThumb = new WidThumb(userOwnedVods.get(i));
        	      SimplePanel sp = new SimplePanel();
        	      sp.setWidth("20%");
        	      sp.add(widThumb);
        	      videoContainer.add(sp);
        	      videoContainer.setWidgetLeftWidth(sp, i * THUMB_WIDTH, Unit.PX, THUMB_WIDTH, Unit.PX);
        	      videoContainer.forceLayout();
        	    }

              showVideoPage(0);
            } else {
            	disclosurePanel.setOpen(false);
            	videoContainer.add(new HTML("NO VIDEO FOUND!"));
            }
          }

          @Override
          public void onFailure(Throwable caught) {
            GWT.log("Failed to load user video.");
          }
        });
  }

	private void showVideoPage(int pager) {
		if (currentPage == 0) {
			previousVideos.setEnabled(false);
		} else {
			previousVideos.setEnabled(true);
		}

		if (currentPage == navButtons.length - 1) {
			nextVideos.setEnabled(false);
		} else {
			nextVideos.setEnabled(true);
		}
		
		if (currentPage == pager) {
			return;
		}
		currentPage = pager;

		int end = (pager + 1) * MAX_VIDEO_COLUMN > userOwnedVods.size() ? userOwnedVods
				.size() : (pager + 1) * MAX_VIDEO_COLUMN;
		
		for (int i = 0; i < videoContainer.getWidgetCount(); i++) {
			videoContainer.setWidgetLeftWidth(videoContainer.getWidget(i), i * THUMB_WIDTH - MAX_VIDEO_COLUMN * THUMB_WIDTH * pager, Unit.PX, THUMB_WIDTH, Unit.PX);
		}
		videoContainer.animate(500, new Layout.AnimationCallback() {
			public void onAnimationComplete() {
			}

			public void onLayout(Layer layer, double progress) {
			}
		});
		for (ToggleButton tbtn : navButtons) {
			tbtn.setDown(false);
		}
		navButtons[pager].setDown(true);
	}

  private void showVideoblock(int pager) {
	    videoContainer.clear();

	    int end = (pager + 1) * MAX_VIDEO_COLUMN > userOwnedVods.size() ? userOwnedVods.size() : (pager + 1) * MAX_VIDEO_COLUMN;
	    for (int i = pager * MAX_VIDEO_COLUMN; i < (pager + 1) * MAX_VIDEO_COLUMN; ++i) {
	      WidThumb widThumb;
	      if (i < end) {
	    	  widThumb = new WidThumb(userOwnedVods.get(i));
	      } else {
	    	  widThumb = new WidThumb(null);
	      }
	      SimplePanel sp = new SimplePanel();
	      NShow fadeVideo = new NShow(sp.getElement());
	      sp.setWidth("20%");
	      sp.add(widThumb);
	      videoContainer.add(sp);
	      fadeVideo.play();
	    }
	    currentPage = pager;
	    for (ToggleButton tbtn : navButtons) {
	    	tbtn.setDown(false);
	    }
	    navButtons[pager].setDown(true);
	    if (currentPage == 0) {
	      previousVideos.setEnabled(false);
	    } else {
	      previousVideos.setEnabled(true);
	    }
	    
	    if (currentPage == navButtons.length - 1) {
	      nextVideos.setEnabled(false);
	    } else {
	      nextVideos.setEnabled(true);
	    }
	  }
  
}
